﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Media3D;
using Grafika_WPF.Helpers;
using Grafika_WPF.Shapes;
using Camera = Grafika_WPF.Components.Camera;
using Light = Grafika_WPF.Components.Light;

namespace Grafika_WPF
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private readonly IList<IShape> shapes;
        private Camera _camera;
        private IList<Light> _lights;
        private int _trianglesCount;
        public MainWindow()
        {
            InitializeComponent();
            shapes = new List<IShape>
                         {

                         };

            _lights = new List<Light>()
                       {
                           new Light(new Vector3D(1,-1,1),0.9 ),
                           new Light(new Vector3D(0,0,-1),0.1),
                           new Light(new Vector3D(0,1,0),0.1),
                           new Light(new Vector3D(1,0,-1),0.5),

                       };
        }

        private void SetValues()
        {

            _camera = new Camera()
                         {
                             Distance = 500,
                             Position = new Point3D
                                            {
                                                X = double.Parse(txtBoxCameraX.Text),
                                                Y = double.Parse(txtBoxCameraY.Text),
                                                Z = double.Parse(txtBoxCameraZ.Text)
                                            }
                         };
            _trianglesCount = int.Parse(txtBoxTriangleCount.Text);

        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {

            canvas.Children.Clear();
            SetValues();

            foreach (var shape in shapes)
            {
                if (shape.Triangles.Count != _trianglesCount)
                    shape.Triangulate(_trianglesCount);

                switch (((ComboBoxItem)cmbBoxRenderType.SelectedItem).Content.ToString())
                {
                    case "Siatka":
                        {
                            TriangleFactory.DrawEmptyTriangles(shape.Triangles, canvas, _camera);
                            break;
                        }
                    case "Płaskie":
                        {
                            TriangleFactory.CreateFlatTriangle(_lights, sldAmbientLight.Value / 10, shape.Triangles, canvas, _camera);
                            break;
                        }
                    case "Phong":
                        {
                            TriangleFactory.CreatePhongTriangles(_lights, sldAmbientLight.Value / 10, shape.Triangles, canvas, _camera);
                            break;
                        }
                }
            }
        }

        private void shapeButton_Click(object sender, RoutedEventArgs e)
        {
            var selectedShape = (ComboBoxItem)cmbShapeType.SelectedItem;
            var selectedColor = ((SolidColorBrush)((ComboBoxItem)cmbShapeColor.SelectedItem).Background).Color;


            if (((string)selectedShape.Content) == "Sfera")
            {
                shapes.Add(new Sphere(new Point3D
                                          {
                                              X = double.Parse(txtShapeX.Text),
                                              Y = double.Parse(txtShapeY.Text),
                                              Z = double.Parse(txtShapeZ.Text)
                                          }, selectedColor, double.Parse(txtShapeR.Text)
                               ));
                lstShapes.Items.Add(new ListViewItem
                                        {
                                            Content = "Sfera",
                                            Background = new SolidColorBrush(selectedColor)
                                        });
            }
            else if (((string)selectedShape.Content) == "Walec")
            {
                shapes.Add(new Cylinder(new Point3D
                {
                    X = double.Parse(txtShapeX.Text),
                    Y = double.Parse(txtShapeY.Text),
                    Z = double.Parse(txtShapeZ.Text)
                }, new Point3D
                {
                    X = double.Parse(txtShapeX2.Text),
                    Y = double.Parse(txtShapeY2.Text),
                    Z = double.Parse(txtShapeZ2.Text)
                }, double.Parse(txtShapeR.Text), selectedColor));

                lstShapes.Items.Add(new ListViewItem()
                {
                    Content = "Walec",
                    Background = new SolidColorBrush(selectedColor)
                });
            }
        }

        private void deleteShape_Click(object sender, RoutedEventArgs e)
        {
            var selectedIndex = lstShapes.SelectedIndex;
            if (selectedIndex != -1)
            {
                shapes.RemoveAt(selectedIndex);
                lstShapes.Items.RemoveAt(selectedIndex);
            }
        }


    }
}
